package com.chenwang.measure.ui.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import com.chenwang.measure.R;
import com.chenwang.measure.base.BaseActivity;
import com.chenwang.measure.model.Constant;
import com.chenwang.measure.model.bean.LocationBean;
import com.chenwang.measure.ui.view.TitleLayout;
import com.chenwang.measure.utils.MyToast;
import com.chenwang.measure.utils.SharePreferenceUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

//精度设置
public class MeasureSettingActivity extends BaseActivity {
    @BindView(R.id.iv_fixed)
    ImageView mIvFixed;
    @BindView(R.id.title)
    TitleLayout mTitle;
    @BindView(R.id.iv_save)
    ImageView mIvSave;
    @BindView(R.id.et_horizontal)
    EditText mEtHorizontal;
    @BindView(R.id.et_vertrcal)
    EditText mEtVertrcal;
    @BindView(R.id.et_offset)
    EditText mEtOffset;
    @BindView(R.id.et_time)
    EditText mEtTime;
    private boolean isFixed = false; //固定解50
    private boolean isSave = true; //自动保存


    @Override
    public int setLayoutId() {
        return R.layout.activity_measure_setting;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        //设置固定解
        int fixed = SharePreferenceUtils.getInt(this,"fixed",0);
        if (fixed == 1){
            mIvFixed.setImageResource(R.mipmap.ic_blue_on);
            isFixed = true;
        }else {
            mIvFixed.setImageResource(R.mipmap.ic_blue_off);
            isFixed = false;
        }
        initSetting();

    }


    //初始化界面数据
    private void initSetting(){
        String h =  SharePreferenceUtils.getString(this,Constant.HORIZONTAL,"0.5");
        String v = SharePreferenceUtils.getString(this,Constant.VERTRCAL ,"2");
        int number = SharePreferenceUtils.getInt(this,Constant.LOCATION_NUMBER, 10);
        mEtHorizontal.setText(h);
        mEtTime.setText(number+"" );
        mEtVertrcal.setText(v );
    }


    @OnClick({R.id.iv_save, R.id.iv_fixed, R.id.btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn://保存
                save();
                break;
            case R.id.iv_fixed:
                //固定解开关
                if (isFixed) {
                    mIvFixed.setImageResource(R.mipmap.ic_blue_off);
                    isFixed = false;
                    MyToast.show("固定解关闭");
                    SharePreferenceUtils.putInt(this,"fixed",0);

                } else {
                    mIvFixed.setImageResource(R.mipmap.ic_blue_on);
                    SharePreferenceUtils.putInt(this,"fixed",1);
                    MyToast.show("固定解开启");
                    isFixed = true;
                }
                break;
            case R.id.iv_save://自动保存
                if (isSave) {
                    mIvSave.setImageResource(R.mipmap.ic_blue_off);
                    isSave = false;

                } else {
                    mIvSave.setImageResource(R.mipmap.ic_blue_on);
                    isSave = true;
                }
                break;

        }
    }

    //保存配置
    private void save (){
        String horizontal = mEtHorizontal.getText().toString();
        String vertrcal   = mEtVertrcal.getText().toString();
     //   String offset = mEtOffset.getText().toString();
        String time = mEtTime.getText().toString();
        if (TextUtils.isEmpty(horizontal)){
            MyToast.show("水平精度不能为空");
        }else if (TextUtils.isEmpty(vertrcal)){
            MyToast.show("垂直精度不能为空");
        }/*else if(TextUtils.isEmpty(offset)){
            MyToast.show("偏移警告不能为空");
        }*/else if(TextUtils.isEmpty(time)){
            MyToast.show("测量次数不能为空");
        }else {
            SharePreferenceUtils.putString(this,Constant.HORIZONTAL,horizontal );
            SharePreferenceUtils.putString(this,Constant.VERTRCAL,vertrcal );//高
            SharePreferenceUtils.putInt(this,Constant.LOCATION_NUMBER,Integer.valueOf(time));
            //SharePreferenceUtils.putInt(this,Cons"",offset );
            finish();
             }

    }


}
